package cn.bugstack.domain.credit.model.aggregate;

import cn.bugstack.domain.credit.event.CreditAdjustSuccessMessageEvent;
import cn.bugstack.domain.credit.model.entity.CreditAccountEntity;
import cn.bugstack.domain.credit.model.entity.CreditOrderEntity;
import cn.bugstack.domain.credit.model.entity.TaskEntity;
import cn.bugstack.domain.credit.model.valobj.TradeNameVO;
import cn.bugstack.domain.credit.model.valobj.TradeTypeVO;
import cn.bugstack.domain.rebate.model.valobj.TaskStateVO;
import cn.bugstack.types.event.BaseEvent;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.RandomStringUtils;

import java.math.BigDecimal;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TradeAggregate {
    private String userId;
    private CreditOrderEntity creditOrderEntity;
    private CreditAccountEntity creditAccountEntity;
//    补偿任务
    private TaskEntity taskEntity;

    public static CreditOrderEntity createCreditOrderEntity(String userId,
                                                           TradeNameVO tradeNameVO,
                                                           TradeTypeVO tradeTypeVO,
                                                           BigDecimal tradeAmount,
                                                           String outBusinessNo){
        return CreditOrderEntity.builder()
                .userId(userId)
                .orderId(RandomStringUtils.randomNumeric(12))// 不需要外部透传，一般用工具在聚合中创建
                .tradeName(tradeNameVO)
                .tradeType(tradeTypeVO)
                .tradeAmount(tradeAmount)
                .outBusinessNo(outBusinessNo)
                .build();
    }

    public static CreditAccountEntity createCreditAccountEntity(String userId, BigDecimal adjustAmount){
        return CreditAccountEntity.builder()
                .userId(userId)
                .adjustAmount(adjustAmount)
                .build();
    }

    public static TaskEntity createTaskEntity(String userId, String topic, String  messageId, BaseEvent.EventMessage<CreditAdjustSuccessMessageEvent.CreditAdjustSuccessMessage> message){
        TaskEntity task = new TaskEntity();
        task.setUserId(userId);
        task.setTopic(topic);
        task.setMessageId(messageId);
        task.setMessage(message);
        task.setState(TaskStateVO.create);
        return task;
    }
}
